package Window;
/*
 * A bemeneti elemeket és értéküket megjelenítő panel. A kapcsolók aktuális értékét 
 * és a jelgenerátorok soron következő értékét láthatjuk a panelen,amelyet 
 * meg is tudunk változtatni. A jelgenerátorokat tartalmazó táblázat soraiban 
 * szereplő gombokra kattintva új ablak nyílik meg, amelyben megadhatjuk az értékeket.
 * */
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
import javax.swing.table.DefaultTableModel;

public class InputPanel {

	//view peldany
	View view;
	
	//ablak meretei
	protected int panelWidth;
	protected int panelHeight;
	protected JPanel inputPanel;
	//elrenedez boxai
	protected Box verticalBoxOfSwitch;
	protected Box verticalBoxOfSignal;
	protected Box horizontalBox;
	
	//tablak
	protected JTable switchTable;
	protected JTable signalTable;
	
	//Konstruktor
	public InputPanel(View view, Dimension windowSize){
		this.view = view;
		
		panelWidth = (int)(windowSize.width*0.5);
		panelHeight = (int)(windowSize.height*0.5);
		initPanelAndBox();
		initSwitch();
		initSignal();
		setHorizontalBox();
		createBorderForPanel();
	}
	
	//Panelt adja vissza
	public JPanel getPanel(){
		return inputPanel;
	}
	
	//panel letrehozas
	private void initPanelAndBox(){
		BorderLayout border = new BorderLayout();
		inputPanel = new JPanel(border);
		
		Dimension panelSize = new Dimension(panelWidth, panelHeight);
		inputPanel.setMaximumSize(panelSize);
		inputPanel.setPreferredSize(panelSize);
		
		horizontalBox = Box.createHorizontalBox();
	}
	
	//Switch tabla lethreozas
	private void initSwitch(){
		verticalBoxOfSwitch = Box.createVerticalBox();
		
		JLabel switchLabel = new JLabel("Switch informations");
		verticalBoxOfSwitch.add(switchLabel);
		
		initSwitchTables();
		JScrollPane switchScrollPane = new JScrollPane(switchTable);
		Dimension switchSize = new Dimension(panelWidth, (int)(panelHeight*0.8));
		switchScrollPane.setMaximumSize(switchSize);
		verticalBoxOfSwitch.add(switchScrollPane);
	}
	
	//Switch JTable letrehozas
	private void initSwitchTables(){
		String[] columnNames = new String[2];
		columnNames[0] = "Switch ID"; columnNames[1] = "LogicalValue";
		
		int[] switchValues = view.getSwitches();
		
		String[][] switchDatas = null;
		if( switchValues != null ){
			switchDatas = new String[switchValues.length][2];
			for(int act=0; act<switchValues.length; act++){
				switchDatas[act][0] = Integer.toString(act); 
				switchDatas[act][1] = Integer.toString(switchValues[act]);
			}
			switchTable = new JTable(switchDatas, columnNames){
				public boolean isCellEditable(int row, int column){
					if( column == 1)
						return true;
					return false;
				}
			};
		}
		else
			switchTable = null;
	}
	
	//A switch ertekeket frissiti a tablakbol
	public void updateSwitchValues(){
		if( switchTable.isEditing() )
			switchTable.getCellEditor().stopCellEditing();
		int[] switchValues = new int[switchTable.getRowCount()];
		for(int act=0; act<switchValues.length; act++){
			try{
				switchValues[act] = Integer.parseInt(switchTable.getValueAt(act, 1).toString());
			}catch(Exception e){
				view.catchException(new Exception("Switch value can be 0 or 1, but received: "+switchTable.getValueAt(act, 1)+" at row: "+act));
			}
			if(switchValues[act] != 0 && switchValues[act] != 1)
				view.catchException(new Exception("Switch value can be 0 or 1, but received: "+switchValues[act]+" at row: "+act));
		}
		view.setSwitches(switchValues);
	}
	
	//Signal tabla letrehozas
	private void initSignal(){
		verticalBoxOfSignal = Box.createVerticalBox();
		
		JLabel signalLabel = new JLabel("Signal informations");
		verticalBoxOfSignal.add(signalLabel);
		
		initSignalTables();
		JScrollPane signalScrollPane = new JScrollPane(signalTable);
		Dimension signalSize = new Dimension(panelWidth, (int)(panelHeight*0.8));
		signalScrollPane.setMaximumSize(signalSize);
		verticalBoxOfSignal.add(signalScrollPane);
	}
	
	//Signal JTable letrehozas
	private void initSignalTables(){
		String[] columnNames = new String[2];
		columnNames[0] = "Signal ID"; columnNames[1] = "Check button";
		
		int[] signalValues = view.getSignal();
		if( signalValues != null ){
			Object[][] dataVector = new Object[signalValues.length][2];
			for(int act=0; act<signalValues.length; act++){
				dataVector[act][0] = Integer.toString(act);
				dataVector[act][1] = "Details";
			}
			
			DefaultTableModel dm = new DefaultTableModel();
			dm.setDataVector(dataVector, columnNames);
			
			signalTable = new JTable(dm){
				public boolean isCellEditable(int row, int column){
					if( column == 1)
						return true;
					return false;
				}
				public boolean isCellSelected(int row, int column){
					return false;
				}
			};
			signalTable.getColumn("Check button").setCellRenderer(new ButtonCellRenderer());
			signalTable.getColumn("Check button").setCellEditor(new ButtonCellEditor());
			
			for(int act=0; act<signalValues.length; act++){
				ButtonCellEditor buttonCellEditor = (ButtonCellEditor)signalTable.getCellEditor(act, 1);
				JButton button = new JButton("Details");
				ALForOpenSignalButton aLForOpenSignalButton = new ALForOpenSignalButton(act);
				button.addActionListener(aLForOpenSignalButton);
				buttonCellEditor.setCellButton(button);
			}
		}
		else
			signalTable = null;
	}
	
	//HorizontalBox letrehozas
	private void setHorizontalBox(){
		
		Dimension spaceOnLeftSide = new Dimension( (int)(panelWidth*0.06), panelHeight);
		horizontalBox.add(Box.createRigidArea(spaceOnLeftSide));
		
		horizontalBox.add(verticalBoxOfSwitch);
		
		Dimension spaceBetweenTables = new Dimension( (int)(panelWidth*0.06), panelHeight);
		horizontalBox.add(Box.createRigidArea(spaceBetweenTables));
		
		horizontalBox.add(verticalBoxOfSignal);
		
		Dimension spaceOnRigthSide = new Dimension( (int)(panelWidth*0.06), panelHeight);
		horizontalBox.add(Box.createRigidArea(spaceOnRigthSide));
		
	}
	
	//border letrehozas
	private void createBorderForPanel(){
		inputPanel.add(horizontalBox);
		inputPanel.setBorder(BorderFactory.createTitledBorder("Switches and SignalGenerators"));
	}
	//A jelgenerátorokhoz tartozó gombokhoz tartozó ActionListenerek
	private class ALForOpenSignalButton implements ActionListener{
		int nbOfSignal;
		
		public ALForOpenSignalButton(int nbOfSignal){
			super();
			this.nbOfSignal = nbOfSignal;
		}
		//kattintáskor létrehozzuk az értékek megjelenítéséhez és módosításához szükséges ablakot
		public void actionPerformed(ActionEvent e){
			SignalModifyWindow signalmod = new SignalModifyWindow(view, nbOfSignal);
		}
	}
}
